<?php
/**
 * 前台使用的 应用 相关数据获取方法
 * Created by PhpStorm.
 * User: samxiao
 * Date: 2017/6/9
 * Time: 下午5:58
 */

namespace App\Repositories;

use App\Models\cms\Category;
use App\Models\cms\Content;
use App\Models\cms\ContentBlog;
use App\Models\cms\ContentPicture;
use App\Models\cms\Navigation;
use App\Models\Course;
use App\Models\CourseCategory;
use App\Models\Property;
use App\Models\Teacher;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Console\Helper\Table;

class AppRepository
{

    private $navigationArr = null;
    /**
     * @var array
     */
    private $properties = null;
    const CACHE_TAG = 'content-list';

    /**
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public function getNav()
    {
        return Navigation::query()->where(['parent_id' => 0])->whereNull('deleted_at')->get();
    }

    /**
     * @param $path
     * @return string
     */
    public function getImageHome($path)
    {
        $thumbBaseUrl = 'http://' . env('IMG_ENDPOINT') . '/';
        return $thumbBaseUrl . $path;
    }

    /**
     * @param     $content
     * @param int $number
     * @return string
     */
    public function standard($content, $number = 20)
    {
        if (mb_strlen($content) > 20) {
            return $this->utf8Substr($content, 1, $number) . '...';
        }
        return $content;
    }

    /**
     * @param $str
     * @param $from
     * @param $len
     * @return null|string|string[]
     */
    private function utf8Substr($str, $from, $len)
    {
        return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $from . '}' .
            '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $len . '}).*#s',
            '$1', $str);
    }

    /**
     * @param $string
     * @return null|string|string[]
     */
    public function getString($string)
    {
        return strip_tags($string);
    }

    /**
     * @param string $type
     * @return array|\Illuminate\Support\Collection
     */
    public function getProperty($type = 'site-property')
    {
        if (empty($this->properties)) {
            $this->properties = Property::query()->where('type', $type)->pluck('value', 'key');
        }
        return $this->properties;
    }

    public function getNameDesc($string)
    {
        return explode('  ', $string);
    }

    /**
     *
     * 获取栏目内容
     * @return null
     */
    public function getContentByColumn($type, $number, $column1, $column2)
    {

        \Cache::flush();
        $value = \Cache::remember('contents' . $type . $column1 . $column2 . $number, 100,
            function () use ($type, $column1, $column2, $number) {
                $querys = Content::query()
                    ->where('content_detail_type', $type)->with(['content_detail'])
                    ->limit($number);
                if ($column1 && $column2) {
                    $columnA = Category::query()->where('name', $column1)->first();
                    $columnB = Category::query()->where('name', $column2)->first();
                    if (!$columnA || !$columnB) {
                        return [];
                    } else {
                        $key = "%{$columnA->id}-%{$columnB->id}%";
                        $querys->whereHas('categories', function ($query) use ($key) {
                            $query->where('selected_label', 'like', $key);
                        });
                    }
                    $model = $querys->orderBy('cms_contents.id')->get();
//                    dd($model);
                    return $model;
                } else {
                    return [];
                }

            });
        return $value;
    }

    public function getNewCourses()
    {
        $value = \Cache::remember('newCourses', 10,
            function () {
                return \DB::table('courses')->orderByDesc('created_at')->limit(6)->get();
            });
        return $value;
    }

    public function getRecommendCourses()
    {
        $value = \Cache::remember('RecommendCourses', 10,
            function () {
                return \DB::table('courses')->where('recommend', 1)->orderBy('created_at')->limit(6)->get();
            });
        return $value;
    }

    public function getRecommendTeachers()
    {
        $value = \Cache::remember('RecommendTeachers', 10,
            function () {
                return Teacher::with('courses')->where('recommend', 1)->orderBy('created_at')->limit(5)->get();
            });
        return $value;
    }

    /**
     * @param $category_id
     * @param $course_id
     * @return mixed
     */
    public function getRecommendCourseByCourseId($category_id,$course_id)
    {
        $value = \Cache::remember('RecommendCategoryCourse' . $course_id, 60,
            function () use ($category_id,$course_id) {
                $result = Course::query()
                    ->where('recommend', 1)
                    ->where('id','!=',$course_id)
                    ->orderByRaw("case category_id  when {$category_id}  then 0 else 99 end ,category_id")
                    ->limit(10)->get();
                return $result;
            });
        return $value;
    }

    /**
     * @param $category_id
     * @param $course_id
     * @return mixed
     */
    public function getRecommendCourse()
    {
        $value = \Cache::remember('RecommendCategoryCourse', 60,
            function () {
                $result = Course::query()
                    ->where('recommend', 1)
                    ->limit(8)->get();
                return $result;
            });
        return $value;
    }

    /**
     * @return mixed
     */
    public function getCourseCategory(){
        $value = \Cache::remember('CategoryCourse', 60,
            function ()  {
                return CourseCategory::query()->select(['name','id'])->get();
            });
        return $value;
    }

}
